<template>
	<view class="stock">
		你超越了魔道
		<button @click="btn">🐒</button>
	</view>
</template>

<script>
export default {
	data() {
		this.heartbeat = null;
		return {
			host: 'ws://testews.sunwaystech.com',
			socket: null, 
			isSuccess: false,		// 是否链接成功
		}
	},
	onLoad() {
		this.init();
	},
	methods: {
		btn() {
			// img = null;
			uni.navigateTo({
				url: '/pages/index/index'
			})
		},
		init() {
			// this.createSocket();
			// setInterval( _ => {
			// 	console.log( '🐲' );
			// },2000)
		},
		createSocket() {
			const token = uni.getStorageSync('token');
			this.socket = wx.connectSocket({
				url: this.host + '/socket.io/?token=' + token,
				header: {
				  'content-type': 'application/json',
				  'AppVersion': '1.2.11'
				},
			});
			this.socket.onClose( () => {
				this.socket = null;
			})
			this.socket.onMessage( (res) => {
				console.log( '🐲',res );
			})
			this.socket.onOpen( () => {
				this.isSuccess = true;
			})
			this.start_heartbeat();
		},
		start_heartbeat() {
			this.heartbeat = setTimeout( () => {
				if ( this.isSuccess ) {
					this.socket.send({
						data: "" + 2
					})
					clearTimeout(this.heartbeat);
				}
			},3000);
		}
	}
}
</script>

<style>
	.stock {
		height: 100vh;
		background: rgba(0,0,0, 0.4);
	}
</style>
